package com.pro.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pro.beans.Alarm;
import com.pro.beans.AlarmTypeResult;
import com.pro.beans.GridPersons;
import com.pro.beans.Page;
import com.pro.beans.Pie;
import com.pro.dao.AlarmDao;
import com.pro.dao.GridPersonsDao;
import com.pro.mybatis.GroupBy;
import com.pro.mybatis.ListPage;
import com.pro.mybatis.MyBatisExpression;
import com.pro.mybatis.SearchCondition;
import com.pro.param.RequestParam;

@Service
public class AlarmService {
	@Resource
	public AlarmDao alarmDao;
	
	@Resource
	public GridPersonsDao gridPersonsDao;

	public List<Alarm> loadAlarmByType(){
		SearchCondition searchCondition = new SearchCondition();
		searchCondition.setSql(" count(id) count ,type");
		searchCondition.setGroupBy(new GroupBy("type"));
		List<Alarm> alarmList = alarmDao.loadAlarmListBySearchCd(searchCondition);
		return alarmList;
	}
	
	public List<Pie> loadPie(){
		List<Pie> pisList = new ArrayList<Pie>();
		List<Alarm> alarmList = this.loadAlarmByType();
		int total = 0;
		for(Alarm alarm:alarmList){
			total += Integer.valueOf(alarm.getCount());
		}
		int i = 0;
		for(Alarm alarm:alarmList){
			Pie pie = new Pie();
			pie.setName(alarm.getTypeName());
			pie.setY(Float.valueOf(alarm.getCount())/total);
			pisList.add(pie);
		}
		return pisList;
	}
	public int[][] loadDataByDays(){
		int[][] array= {{0,0,0,0,0,
					0,0,0,0,0,
					0,0,0,0,0,
					0,0,0,0,0,
					0,0,0,0,0,
					0,0,0,0,0,0},
					{0,0,0,0,0,
						0,0,0,0,0,
						0,0,0,0,0,
						0,0,0,0,0,
						0,0,0,0,0,
						0,0,0,0,0,0},
						{0,0,0,0,0,
							0,0,0,0,0,
							0,0,0,0,0,
							0,0,0,0,0,
							0,0,0,0,0,
							0,0,0,0,0,0}};
		List<Alarm> fireList = alarmDao.loadDataByDays(1);
		List<Alarm> deviceList = alarmDao.loadDataByDays(2);
		List<Alarm> elecList = alarmDao.loadDataByDays(3);
		for(Alarm alarm:fireList){
			Integer day = alarm.getDays();
			array[0][day-1]=Integer.valueOf(alarm.getCount());
		}
		for(Alarm alarm:deviceList){
			Integer day = alarm.getDays();
			array[1][day-1]=Integer.valueOf(alarm.getCount());
		}
		for(Alarm alarm:elecList){
			Integer day = alarm.getDays();
			array[2][day-1]=Integer.valueOf(alarm.getCount());
		}
		return array;
	}

	public List<Alarm> getObjectsByPage(Page page) {
		return alarmDao.getObjectsByPage(page);
	}

	// 分页参数获取
	public List<Alarm> loadAlarmListBySearch(RequestParam requestParam) {
		return alarmDao.loadAlarmListBySearchCd(new SearchCondition());
	}

	public ListPage loadAlarmList(RequestParam requestParam) {
		ListPage page = requestParam.getPage();
		SearchCondition sc = new SearchCondition();
		if (page == null || page.getTotal() == null) {
			page = new ListPage<Alarm>();
		}
		sc.and(MyBatisExpression.eq("type", requestParam.getType()));
		page.setTotal(alarmDao.getTotal(sc));
		sc.and(MyBatisExpression.bt("alarm_time",
				requestParam.getStart_alarm_time(), true));
		sc.and(MyBatisExpression.lt("alarm_time",
				requestParam.getEnd_alarm_time(), true));
		sc.and(MyBatisExpression.like("company_name",
				requestParam.getKey_word()));
		sc.or(MyBatisExpression.like("p_desc", requestParam.getKey_word()));
		sc.setPage(page);
		List<Alarm> alarmList = alarmDao.loadAlarmListBySearchCd(sc);
		page.setDataList(alarmList);
		return page;
	}

	public List<AlarmTypeResult> searchAlarmByType() {
		SearchCondition sc = new SearchCondition();
		sc.setGroupBy(new GroupBy("type"));
		sc.setSql(" count(\"id\") count,type ");
		List<Alarm> alarmList = alarmDao.loadAlarmListBySearchCd(sc);
		List<AlarmTypeResult> alarmTypeResults = new ArrayList<AlarmTypeResult>();
		for (Alarm alarm : alarmList) {
			alarmTypeResults.add(new AlarmTypeResult(alarm.getCount(), alarm
					.getType()));
		}
		return alarmTypeResults;
	}

	public int addAlarm(Alarm alarm) {
		return alarmDao.addObject(alarm);
	}

	public int update(Alarm alarm) {
		return alarmDao.updateObject(alarm);
	}

	public Alarm getAlarmById(Integer id) {
		Alarm alarm = alarmDao.getObject(id);
		GridPersons gridPersons = new GridPersons();
		gridPersons.setGrid_id(alarm.getGrid_id());
		List<GridPersons> gridPersonsList = gridPersonsDao.getObjectsByObject(gridPersons);
		if(gridPersonsList!=null && gridPersonsList.size()>0){
			alarm.setGridPersons(gridPersonsList.get(0));
		}
		return alarm;
	}

	public int deleteAlarm(Integer id) {
		Alarm alarm = new Alarm();
		alarm.setId(id);
		alarm.setDel_flag(1);
		return alarmDao.updateObject(alarm);
	}
}
